package com.wenhui.schoolyardinfo.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/12 13:45
 * introduce :
 */

public class AlbumPagerAdapter extends PagerAdapter {

    private Context context;
    private List<String> photoUrlList = new ArrayList<>();
    private PhotoView photoView;

    public AlbumPagerAdapter(Context context) {
        this.context = context;
    }

    public AlbumPagerAdapter(Context context, List<String> photoUrlList) {
        this.context = context;
        this.photoUrlList = photoUrlList;
    }

    public void clearData() {
        photoUrlList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<String> photoUrlList) {
        this.photoUrlList.addAll(photoUrlList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        photoView = new PhotoView(container.getContext());
        Glide.with(context).load(photoUrlList.get(position)).into(photoView);
        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return photoUrlList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        photoView = (PhotoView) object;
    }

    public PhotoView getPhotoView() {
        return photoView;
    }
}
